package com.xiaoyu.lingdian.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.xiaoyu.lingdian.controller.BaseController;
import com.xiaoyu.lingdian.core.mybatis.page.Page;
import com.xiaoyu.lingdian.entity.CoreAttachment;
import com.xiaoyu.lingdian.entity.CoreIntegral;
import com.xiaoyu.lingdian.entity.CorePointsProduct;
import com.xiaoyu.lingdian.service.CoreAttachmentService;
import com.xiaoyu.lingdian.service.CoreIntegralService;
import com.xiaoyu.lingdian.service.CorePointsProductService;
import com.xiaoyu.lingdian.tool.RandomUtil;
import com.xiaoyu.lingdian.tool.out.ResultMessageBuilder;
import com.xiaoyu.lingdian.vo.CorePointsProductVO;

@Controller
@RequestMapping(value="/corePointsProduct")
public class CorePointsProductController extends BaseController {

	/**
	 * 商品表
	 */
	@Autowired
	private CorePointsProductService corePointsProductService;
	
	/**
	 * 附件表
	 */
	@Autowired
	private CoreAttachmentService coreAttachmentService;
	
	/**
	 * 积分攻略表
	 */
	@Autowired
	private CoreIntegralService coreIntegralService;
	
	/**
	 *根据商店名称分页查询商品列表
	 * 
	 * @param crpopTitle
	 * @param crpopArea 版块UUID
	 * @param pageNum
	 * @param pageSize
	 * @param response
	 */
	@RequestMapping(value = "/find/by/cnd", method = RequestMethod.POST)
	public void findByCnd(String crpopTitle, String crpopArea, Integer pageNum, Integer pageSize, HttpServletResponse response) {
		logger.info("[CorePointsProductController.findByCnd]:begin findByCnd.");
		if (pageNum == null) {
			pageNum = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		int points = 0;
		CoreIntegral coreIntegral = new CoreIntegral();
		coreIntegral.setCriglUuid("BBBBB");
		coreIntegral = coreIntegralService.getCoreIntegral(coreIntegral);
		if (coreIntegral != null) {
			String criglScore = coreIntegral.getCriglScore();
			points = Integer.valueOf(criglScore.substring(0, criglScore.length()-1));    //1分
		}	 
		Page<CorePointsProduct> page = corePointsProductService.findCorePointsProductByCnd(crpopTitle, crpopArea, pageNum, pageSize);
		Page<CorePointsProductVO> pageVO = new Page<>(page.getPageNumber(), page.getPageSize(), page.getTotalCount());		
		List<CorePointsProductVO> list = new ArrayList<>();
		CorePointsProductVO vo = new CorePointsProductVO();
		for (CorePointsProduct corePointsProduct : page.getResult()) {
			vo = new CorePointsProductVO();
			vo.convertPOToVO(corePointsProduct);
			double fee = 0.0;
			try {
				fee = Double.parseDouble(vo.getCrpopFee());
			} catch (Exception e) {
			}
			long a = Math.round((points * fee) / 10.0);
			vo.setKhdScore(Integer.parseInt(a+"")); 
			//根据商品UUID查询图片附件
			List<CoreAttachment> attachmentslist = coreAttachmentService.findCoreAttachmentByCnd(corePointsProduct.getCrpopUuid());
			vo.setList(attachmentslist);
			list.add(vo);
		}
		pageVO.setResult(list);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "查询商品成功", pageVO), response);
		logger.info("[CorePointsProductController.findByCnd]:end findByCnd.");
	}
	
	/**
	 * 添加商品(添加附件)
	 * 
	 * @param crpopTitle 标题
	 * @param crpopFee 价格
	 * @param crpopStock 库存
	 * @param crpopDesc 简介
	 * @param crpopArea 所属版块
	 * @param uuids
	 * @param response
	 */
	@RequestMapping(value="/add/pointsproduct", method=RequestMethod.POST)
	public void addPointsProduct(String crpopTitle, String crpopFee, Integer crpopStock, String crpopDesc,
			String crpopArea, String uuids,HttpServletResponse response) {
		logger.info("[CorePointsProductController.addPointsProduct]:begin addPointsProduct");	
		if (StringUtils.isBlank(crpopTitle)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入商品名称！"), response);
			return;
		}
		if (StringUtils.isBlank(crpopFee)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入商品价格！"), response);
			return;
		}
		if (("0").equals(crpopFee)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "商品价格不能为0！"), response);
			return;
		}
		if (null == crpopStock) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入商品库存！"), response);
			return;
		}
		if (StringUtils.isBlank(crpopArea)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请选择所属版块！"), response);
			return;
		}
		CorePointsProduct corePointsProduct = new CorePointsProduct();
		String uuid = RandomUtil.generateString(16);
		corePointsProduct.setCrpopUuid(uuid);		
		corePointsProduct.setCrpopCdate(new Date());		
		corePointsProduct.setCrpopStatus(1);
		corePointsProduct.setCrpopDesc(crpopDesc);
		corePointsProduct.setCrpopFee(crpopFee);
		corePointsProduct.setCrpopOrd(1);
		corePointsProduct.setCrpopStock(crpopStock);
		corePointsProduct.setCrpopTitle(crpopTitle);
		corePointsProduct.setCrpopArea(crpopArea);
		corePointsProductService.insertCorePointsProduct(corePointsProduct);	
		// 添加商品封面
		if (!StringUtils.isBlank(uuids)) {
			CoreAttachment coreAttachment = new CoreAttachment();
			String[] uuidModel = uuids.split("\\|");
			for (int i = 0; i < uuidModel.length; i++) {
				coreAttachment = new CoreAttachment();
				coreAttachment.setCratmUuid(uuidModel[i]);
				coreAttachment.setCratmBusUuid(uuid);
				coreAttachmentService.updateCoreAttachment(coreAttachment);
			}
		}
				
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "添加商品成功！",uuid), response);
		logger.info("[CorePointsProductController.addPointsProduct]:end addPointsProduct");
	}
	
	/**
	 * 修改商品
	 * 
	 * @param crpopTitle 标题
	 * @param crpopFee 价格
	 * @param crpopStock 库存
	 * @param crpopDesc 简介
	 * @param crpopArea 所属版块
	 * @param uuids
	 * @param response
	 */
	@RequestMapping(value="/update/pointsproduct", method=RequestMethod.POST)
	public void updatePointsProduct(String crpopUuid, String crpopTitle, String crpopFee, Integer crpopStock,
			String crpopDesc, String crpopArea, String uuids, HttpServletResponse response) {
		logger.info("[CorePointsProductController.updatePointsProduct]:begin updatePointsProduct");
		if (StringUtils.isBlank(crpopUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "缺少crpopUuid参数！"), response);
			return;
		}
		if (StringUtils.isBlank(crpopTitle)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入商品名称！"), response);
			return;
		}
		if (StringUtils.isBlank(crpopFee)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入商品价格！"), response);
			return;
		}
		if (("0").equals(crpopFee)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "商品价格不能为0！"), response);
			return;
		}
		if (null == crpopStock) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入商品库存！"), response);
			return;
		}
		if (StringUtils.isBlank(crpopArea)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请选择所属版块！"), response);
			return;
		}
		CorePointsProduct corePointsProduct = new CorePointsProduct();
		corePointsProduct.setCrpopUuid(crpopUuid);		
		corePointsProduct.setCrpopUdate(new Date());	
		corePointsProduct.setCrpopDesc(crpopDesc);
		corePointsProduct.setCrpopFee(crpopFee);
		corePointsProduct.setCrpopStock(crpopStock);
		corePointsProduct.setCrpopTitle(crpopTitle);
		corePointsProduct.setCrpopArea(crpopArea);
		corePointsProductService.updateCorePointsProduct(corePointsProduct);
		// 添加商品封面
		if (!StringUtils.isBlank(uuids)) {
			CoreAttachment coreAttachment = new CoreAttachment();
			String[] uuidModel = uuids.split("\\|");
			for (int i = 0; i < uuidModel.length; i++) {
				coreAttachment = new CoreAttachment();
				coreAttachment.setCratmUuid(uuidModel[i]);
				coreAttachment.setCratmBusUuid(crpopUuid);
				coreAttachmentService.updateCoreAttachment(coreAttachment);
			}
		}
				
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "更新商品成功！"), response);
		logger.info("[CorePointsProductController.updatePointsProduct]:end updatePointsProduct");
	}
	
	/**
	 * 查看商品
	 * 
	 * @param crpopUuid
	 * @param response
	 */
	@RequestMapping(value="/views", method=RequestMethod.POST)
	public void viewsPointsProduct(String crpopUuid, HttpServletResponse response) {
		logger.info("[CorePointsProductController.viewsPointsProduct]:begin viewsPointsProduct");
		if (StringUtils.isBlank(crpopUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "缺少crpopUuid参数！"), response);
			return;
		}
		CorePointsProduct corePointsProduct = new CorePointsProduct();
		corePointsProduct.setCrpopUuid(crpopUuid);
		corePointsProduct = corePointsProductService.getCorePointsProduct(corePointsProduct);
		CorePointsProductVO vo = new CorePointsProductVO();
		vo.convertPOToVO(corePointsProduct);
		int points = 0;
		CoreIntegral coreIntegral = new CoreIntegral();
		coreIntegral.setCriglUuid("BBBBB");
		coreIntegral = coreIntegralService.getCoreIntegral(coreIntegral);
		if (coreIntegral != null) {
			String criglScore = coreIntegral.getCriglScore();
			points = Integer.valueOf(criglScore.substring(0, criglScore.length()-1));    //1分
		}
		double fee = 0.0;
		try {
			fee = Double.parseDouble(vo.getCrpopFee());
		} catch (Exception e) {
		}
		long a = Math.round((points * fee) / 10.0);
		vo.setKhdScore(Integer.parseInt(a+""));
		//根据商品UUID查询图片附件
		List<CoreAttachment> attachmentslist = coreAttachmentService.findCoreAttachmentByCnd(corePointsProduct.getCrpopUuid());
		vo.setList(attachmentslist);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "查看商品详情成功！", vo), response);
		logger.info("[CorePointsProductController.viewsPointsProduct]:end viewsPointsProduct");
	}
	
	/**
	 * 删除商品（彻底删除）
	 * 
	 * @param crpopUuid
	 * @param response
	 */
	@RequestMapping(value="/delete/one", method=RequestMethod.POST)
	public void deleteOne(String crpopUuid, HttpServletResponse response) {
		logger.info("[CorePointsProductController.deleteOne]:begin deleteOne");
		if (StringUtils.isBlank(crpopUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "缺少crpopUuid参数！"), response);
			return;
		}
		CorePointsProduct corePointsProduct = new CorePointsProduct();
		corePointsProduct.setCrpopUuid(crpopUuid);
		corePointsProductService.deleteCorePointsProduct(corePointsProduct);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "删除商品成功！"), response);
		logger.info("[CorePointsProductController.deleteOne]:end deleteOne");
	}
	
	/**
	 * 批量删除商品
	 * 
	 * @param crpopUuids
	 * @param response
	 */
	@RequestMapping(value="/delete/batch", method=RequestMethod.POST)
	public void deleteBatch(String crpopUuids, HttpServletResponse response) {
		logger.info("[CorePointsProductController.deleteBatch]:begin deleteBatch");
		if (StringUtils.isBlank(crpopUuids)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请选择批量删除对象！"), response);
			return;
		}
		String[] array = crpopUuids.split("\\|");
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < array.length; i++) {
			list.add(array[i]);
		}
		if (list.size() == 0) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请选择批量删除对象！"), response);
			return;
		}
		corePointsProductService.deleteCorePointsProductByCnd(list);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "删除商品成功！"), response);
		logger.info("[CorePointsProductController.deleteBatch]:end deleteBatch");
	}
	
}